﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>

    <div>
        <link href="../../kindeditor/themes/default/default.css" rel="stylesheet" />
        <link href="../../kindeditor/plugins/code/prettify.css" rel="stylesheet" />
        <script src="../../kindeditor/kindeditor-all.js"></script>
        <script src="../../kindeditor/lang/zh-CN.js"></script>
        <script src="../../kindeditor/plugins/code/prettify.js"></script>

        <table class="easyui-datagrid" id="newlist" data-options="rownumbers:true,toolbar:'#newstb'">

        </table>

        <div id="newstb" style="padding: 2px 5px;">
            <div id="ft" style="padding: 2px 5px;">
                <a href="#" class="easyui-linkbutton" iconcls="icon-add" onclick="addnew()" plain="true">添加新闻</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-edit" onclick="bt_editnew_click()" plain="true">编辑选中</a>
                <a href="#" class="easyui-linkbutton" iconcls="icon-remove" onclick="delnew()" plain="true">删除选中</a>
            </div>
        </div>


        <div id="newsedigwin" class="easyui-dialog" title="编辑" style="width: 850px; height: 600px;"
            data-options="iconCls:'icon-edit',resizable:true,modal:true,buttons:'#edit_bt',closed: true">
            <div style="margin: 0 auto; text-align: center;">
                <textarea id="NewsEdit" cols="100" rows="8" style="width: 785px; height: 520px; visibility: hidden;" runat="server">
                </textarea>
            </div>
        </div>

        <div id="edit_bt">
            <a href="#" class="easyui-linkbutton" onclick="bt_editnew()">保存</a>
            <a href="#" class="easyui-linkbutton"onclick="$('#newsedigwin').dialog('close')">关闭</a>
        </div>



        <div id="newsaddwin" class="easyui-dialog" title="添加" style="width: 850px; height: 600px;"
            data-options="iconCls:'icon-add',resizable:true,modal:true,buttons:'#add_bt',closed: true">
            <div style="margin: 0 auto; text-align: center;">
                新闻标题：<input type="text" id="newtitle" style="width: 300px">
                请选择您要管理的提示项：
        <input id="newstype" class="easyui-combobox" name="newstype"
            data-options="valueField:'ID',textField:'title',url:'../admin/GetNewsTypeList'"/>
                <textarea id="NewsAdd" cols="100" rows="8" style="width: 770px; height: 520px; visibility: hidden;" runat="server">
                </textarea>
            </div>
        </div> 

         <div id="add_bt">
            <a href="#" class="easyui-linkbutton"onclick="bt_addnew()">保存</a>
            <a href="#" class="easyui-linkbutton" onclick="$('#newsaddwin').dialog('close')">关闭</a>
        </div>

    </div>

    <script type="text/javascript">
        $('#newlist').datagrid({
            url: '../admin/GetNewsList',
            singleSelect: true,
            columns: [[
                { field: 'Expr1', title: '新闻所属', width: 200 },
                { field: 'title', title: '新闻标题', width: 200 },
            { field: 'time', title: '发表时间', width: 200 },
            { field: 'username', title: '作者', width: 200 },
                {
                    field: 'opt', title: '修改操作', width: 100, align: 'center',
                    formatter: function (value, rec) {
                        var btn = "<a href=" + "#" + " class=" + "easyui-linkbutton" + " iconcls=" + "icon-edit" + " onclick=" + "editnew("+rec.ID+")" + " plain=" + "true" + ">编辑</a>";
                        return btn;
                    }
                }
            ]]
        });

        var editoredit;
        KindEditor.ready(function (K) {
            editoredit = K.create('#NewsEdit', { 
                cssPath: '../../kindeditor/plugins/code/prettify.css',
                uploadJson: '../../kindeditor/asp.net/upload_json.ashx',
                fileManagerJson: '../../kindeditor/asp.net/file_manager_json.ashx',
                allowFileManager: true,
                allowMediaUpload: false,
                afterBlur: function () { this.sync(); }
            });
            prettyPrint();
        });

        var editoradd;
        KindEditor.ready(function (aK) {
            editoradd = aK.create('#NewsAdd', {
                cssPath: '../../kindeditor/plugins/code/prettify.css',
                uploadJson: '../../kindeditor/asp.net/upload_json.ashx',
                fileManagerJson: '../../kindeditor/asp.net/file_manager_json.ashx',
                allowFileManager: true,
                allowMediaUpload: false,
                afterBlur: function () { this.sync(); }
            });
            prettyPrint();
        });

        function bt_editnew() {
            var row = $('#newlist').datagrid('getSelected');
            var htm = editoredit.html();
            $.post("../admin/UpdataNews", { ID: row.ID, text: htm},
              function (data) {
                  if (data == false) {
                      $.messager.alert('未知错误', '修改失败.');
                  }
                  else {
                      $.messager.alert('提示', '修改成功');
                      $('#newsedigwin').dialog('close');
                      $('#newlist').datagrid('load', {
                          code: '01',
                          name: 'name'
                      });
                  }
              });
        };

        function bt_editnew_click() {
            var row = $('#newlist').datagrid('getSelected');
            if (row == null) {
                $.messager.alert('提示', '请选择需要修改的新闻');
            } else if (row.length > 1) {
                $.messager.alert('提示', '只能同时修改一行数据，请重新选择');
            } else {
                editnew(row.ID);
            }
        };

        function editnew(ID) {
            $.post("/admin/GetNewsTypeTitle", { "id": ID },
                     function (data) {
                         if (data != "0") {
                             editoredit.val(data);
                         }
                     });
              $('#newsedigwin').dialog('open');
        };
     
        function bt_addnew() {
            var type = $('#newstype').combobox('getValue');
            var title = $('#newtitle').val();
            var htm = editoradd.html();
            $.post("../admin/addNews", { title: title, text: htm, type: type },
              function (data) {
                  if (data == false) {
                      $.messager.alert('未知错误', '添加失败.');
                  }
                  else {
                      $.messager.alert('提示', '添加成功');
                      $('#newsaddwin').dialog('close');
                      $('#newlist').datagrid('load', {
                          code: '01',
                          name: 'name'
                      });
                  }
              });
        }

        function addnew() {
                $('#newsaddwin').dialog('open');
        };

        function delnew() {
            var row = $('#newlist').datagrid('getSelected');
            if (row == null) {
                $.messager.alert('提示', '请选择需要删除的新闻');
            } else if (row.length > 1) {
                $.messager.alert('提示', '只能同时删除一行数据，请重新选择');
            } else {
                $.post("../admin/DeletNews", { ID: row.ID },
               function (data) {
                   if (data == false) {
                       $.messager.alert('未知错误', '删除失败.');
                   }
                   else {
                       $.messager.alert('提示', '删除成功');
                       $('#newlist').datagrid('load', {
                           code: '01',
                           name: 'name'
                       });
                   }
               });
            }
        };
    </script>
</body>
</html>
